package com.xinchuang.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xinchuang.comment.R;
import com.xinchuang.service.TypeService;
import com.xinchuang.utils.CheckUtil;
import com.xinchuang.utils.FileLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import com.xinchuang.utils.DateUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;

@Service
public class TypeServiceImpl implements TypeService {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private FileLoader fileLoader;

    @Override
    public String typeList() {
        JSONArray array = new JSONArray();
        try {
            List<Path> paths = fileLoader.getAllFiles();
            int count = 1;
            for (Path path : paths) {
                JSONObject json = new JSONObject();
                json.put("tarId", count);
                String name = String.valueOf(path.getFileName());
                json.put("tarName", name.substring(0, name.lastIndexOf(".")));
                json.put("tarUrl", "/type/" + path.getFileName());
                array.add(json);
                count++;
            }
            return JSON.toJSONString(R.data(array));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JSON.toJSONString(R.success("无数据"));
    }

}
